# encoding: utf-8
# @Author: zsl
# @Desc : 年龄和性别检测 这个项目face_recognition python3.7.9版本不支持
# @Date  : 2025/07/09 16:45
import face_recognition
import cv2
import numpy as np
import datetime

def detect_age_gender(frame):
    face_locations = face_recognition.face_locations(frame)
    for top, right, bottom, left in face_locations:
        face_image = frame[top:bottom, left:right]
        # 使用深度学习模型进行性别和年龄预测
        # 这里使用示例值，实际应用中需使用专业模型进行预测
        gender = np.random.choice(['Male', 'Female'])
        age = np.random.randint(18, 60)
        cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)
        cv2.putText(frame, f'{gender}, {age} years old', (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
    return frame

cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()
    if ret:
        result_frame = detect_age_gender(frame)
        cv2.imshow('Age and Gender Detection', result_frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

